﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace daStaticAnalyser.Analysers
{
    using daStaticAnalyser;
    using daBase.Objects;
    using daBase.Analyser;
    using daSyntaxAnalyser;
    using daSyntaxAnalyser.SyntaxTree;

    [AnalyserDefaultSettings("М мера", "test", 0, "test")]
    class MAnalyzer : BaseStaticAnalyser
    {
        int GetData(ISyntaxTreeElement elt)
        {
            int ret = 0;
            if (elt is StatementsElement)
            {
                foreach (var elt2 in elt)
                {
                    ret += GetData(elt2);
                }
            }
            else if ((elt is AssignmentElement) ||
                     (elt is VariableStatementElement) ||
                     (elt is GotoStatementElement) ||
                     (elt is ExceptStatementElement))
            {
                ret = 1;
            }
            else if (elt is TestStatementElement)
            {
                var TestSE = elt as TestStatementElement;
                int ThenVal = GetData(TestSE.IfTrue);
                ret = 2*ThenVal;
                if (TestSE.IfFalse != null)
                {
                    int ElseVal = GetData(TestSE.IfFalse);
                    ret = 2*Math.Max(ThenVal, ElseVal);
                }
            }
            else if (elt is CaseStatementElement)
            {
                var CaseSE = elt as CaseStatementElement;
                List<int> CaseVals = new List<int>();
                foreach (var CaseBlock in CaseSE.Cases)
                {
                    CaseVals.Add(GetData(CaseBlock));
                }

                if (CaseVals.Count > 0)
                {
                    int maxval = CaseVals[0];
                    foreach (int val in CaseVals) if (val > maxval) maxval = val;
                    ret = 2 * maxval;
                }
            }
            else if (elt is WhileStatementElement)
            {
                var WhileSE = elt as WhileStatementElement;
                ret = 2*GetData(WhileSE.Statement);
            }
            else if (elt is RepeatStatementElement)
            {
                var RepeatSE = elt as RepeatStatementElement;
                ret = 2 * GetData(RepeatSE.Statements[0]);
            }
            else if (elt is ForStatementElement)
            {
                var ForSE = elt as ForStatementElement;
                ret = 2 * GetData(ForSE.Statement);
            }
            else if ((elt is CompoundStatementElement) ||
                     (elt is CompoundStatementElement) ||
                     (elt is CaseBlockStatementElement))
            {
                foreach (ISyntaxTreeElement element in elt)
                    ret = GetData(element);
            }
            return ret;
        }
        
        protected override void DoAnalyse(SyntaxTreeArgs e)
        {
            String MethodName = "";
            if (CheckMethod_GetName(e, out MethodName))
            {
                OuterMethodElement Method = e.Element as OuterMethodElement;
                int M = GetData(Method.Body.Statement);
                AddMetric("М мера", M,
                    MethodName,
                    Method.StartInfo.File,
                    Method.StartInfo.Line);
            }
        }
    }

    
}
